package com.viyu.alianlk.icons.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.viyu.alianlk.icons.R;
import com.viyu.alianlk.icons.views.LianlkView;
import com.viyu.alianlk.icons.views.listener.OnHelpUsedListener;
import com.viyu.alianlk.icons.views.listener.OnStateChangedListener;
import com.viyu.alianlk.icons.views.listener.OnTimingListener;

/**
 * 
 * @author Viyu_Lu
 * 
 */
public class PlayBoardActivity extends Activity implements OnClickListener, OnTimingListener, OnStateChangedListener, OnHelpUsedListener {

	private final int DEFAULT_XIPAI_COUNT = 3;
	private final int DEFAULT_TISHI_COUNT = 3;

	private LianlkView lianlkView = null;
	//
	private ProgressBar progress = null;
	private TextView timeLeftTextView = null;

	private Button pauseButton = null;
	private boolean isPause = false;
	private Button refreshButton = null;
	private Button hintButton = null;
	private TextView refreshText = null;
	private TextView hintText = null;

	private int diffType = -1;

	// private DomobInterstitialAd ad = null; 插屏ad，没地方放

	public void pauseGame() {// TODO 暂停的时候可以放插屏广告
		/*
		 * // 显示广告 if (ad.isInterstitialAdReady()) {
		 * ad.showInterstitialAd(this); } else { ad.loadInterstitialAd(); }
		 */
	}

	private void writeRecord(int record) {
		//
		SharedPreferences prefers = getSharedPreferences(StartBoardActivity.KEY_SHAREDPREFERENCE_RECORDS, Context.MODE_PRIVATE);
		Editor edit = prefers.edit();
		switch (diffType) {
		case StartBoardActivity.DIFFTYPE_JIANDAN: {
			int old = prefers.getInt(StartBoardActivity.KEY_SHAREDPREFERENCE_RECORDS_JIANDAN, -1);
			if (old < 1 || record < old) {
				edit.putInt(StartBoardActivity.KEY_SHAREDPREFERENCE_RECORDS_JIANDAN, record);
			}
			break;
		}
		case StartBoardActivity.DIFFTYPE_ZHONGDENG: {
			int old = prefers.getInt(StartBoardActivity.KEY_SHAREDPREFERENCE_RECORDS_ZHONGDENG, -1);
			if (old < 1 || record < old) {
				edit.putInt(StartBoardActivity.KEY_SHAREDPREFERENCE_RECORDS_ZHONGDENG, record);
			}
			break;
		}
		case StartBoardActivity.DIFFTYPE_GAOJI: {
			int old = prefers.getInt(StartBoardActivity.KEY_SHAREDPREFERENCE_RECORDS_GAOJI, -1);
			if (old < 1 || record < old) {
				edit.putInt(StartBoardActivity.KEY_SHAREDPREFERENCE_RECORDS_GAOJI, record);
			}
			break;
		}
		case StartBoardActivity.DIFFTYPE_TIAOZHAN: {
			int old = prefers.getInt(StartBoardActivity.KEY_SHAREDPREFERENCE_RECORDS_TIAOZHAN, -1);
			if (old < 1 || record < old) {
				edit.putInt(StartBoardActivity.KEY_SHAREDPREFERENCE_RECORDS_TIAOZHAN, record);
			}
			break;
		}
		}
		edit.commit();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//
		setContentView(R.layout.activity_playboard);
		//
		// load ad
		/*
		 * ad = new DomobInterstitialAd(this, getString(R.string.ads_publishid),
		 * getString(R.string.ads_inlineid),
		 * DomobInterstitialAd.INTERSITIAL_SIZE_300X250);
		 * ad.setInterstitialAdListener(new DomobInterstitialAdListener() {
		 * 
		 * @Override public void onInterstitialAdReady() { }
		 * 
		 * @Override public void onLandingPageOpen() { }
		 * 
		 * @Override public void onLandingPageClose() { }
		 * 
		 * @Override public void onInterstitialAdPresent() { }
		 * 
		 * @Override public void onInterstitialAdFailed(ErrorCode arg0) { }
		 * 
		 * @Override public void onInterstitialAdLeaveApplication() { }
		 * 
		 * @Override public void onInterstitialAdClicked(DomobInterstitialAd
		 * arg0) { }
		 * 
		 * @Override public void onInterstitialAdDismiss() {
		 * ad.loadInterstitialAd(); }
		 * 
		 * }); ad.loadInterstitialAd();
		 */
		//
		diffType = getIntent().getIntExtra(StartBoardActivity.KEY_INTENT_DIFFTYPE, StartBoardActivity.DIFFTYPE_JIANDAN);
		//
		refreshButton = (Button) findViewById(R.id.playboard_refreshbutton);
		hintButton = (Button) findViewById(R.id.playboard_hintbutton);
		refreshText = (TextView) findViewById(R.id.playboard_refreshnum);
		hintText = (TextView) findViewById(R.id.playboard_hintnum);
		refreshButton.setOnClickListener(this);
		hintButton.setOnClickListener(this);
		refreshButton.setText(getString(R.string.text_xipai, DEFAULT_XIPAI_COUNT));
		hintButton.setText(getString(R.string.text_tishi, DEFAULT_TISHI_COUNT));
		//
		Button backButton = (Button) findViewById(R.id.playboard_backbutton);
		backButton.setOnClickListener(this);
		pauseButton = (Button) findViewById(R.id.playboard_pausebutton);
		pauseButton.setOnClickListener(this);

		//
		lianlkView = (LianlkView) findViewById(R.id.playboard_lianlkview);
		lianlkView.setOnTimerListener(this);
		lianlkView.setOnStateListener(this);
		lianlkView.setOnToolsChangedListener(this);
		LianlkView.initSound(this);
		//
		timeLeftTextView = (TextView) findViewById(R.id.playboard_timeleftview);
		timeLeftTextView.setText(lianlkView.getTotalTime() + "s");
		//
		progress = (ProgressBar) findViewById(R.id.playboard_timeleftprogress);
		progress.setMax(lianlkView.getTotalTime());
		//
		// start game
		Animation transIn = AnimationUtils.loadAnimation(this, R.anim.trans_in);

		refreshButton.startAnimation(transIn);
		hintButton.startAnimation(transIn);
		lianlkView.startAnimation(transIn);
		lianlkView.initPlay();
	}

	@Override
	protected void onPause() {
		super.onPause();
		lianlkView.setMode(LianlkView.STATE_PAUSE);
	}

	@Override
	protected void onResume() {
		super.onResume();
		lianlkView.startPlay();
	}

	@Override
	public void onBackPressed() {
	}

	@Override
	protected void onDestroy() {
		clearResToQuit();
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.playboard_refreshbutton: {
			Animation shake01 = AnimationUtils.loadAnimation(this, R.anim.shake);
			refreshButton.startAnimation(shake01);
			lianlkView.refreshChange();
			break;
		}
		case R.id.playboard_hintbutton: {
			Animation shake02 = AnimationUtils.loadAnimation(this, R.anim.shake);
			hintButton.startAnimation(shake02);
			lianlkView.autoClear();
			break;
		}
		case R.id.playboard_backbutton: {
			finish();
			break;
		}
		case R.id.playboard_pausebutton: {
			if(!isPause) {
				isPause = true;
				lianlkView.setMode(LianlkView.STATE_PAUSE);
				pauseButton.setText(R.string.text_goon);
			} else {
				isPause = false;
				pauseButton.setText(R.string.text_pause);
				lianlkView.startPlay();
			}
			
			break;
		}
		}
	}

	final Handler myHandler = new Handler();
	private int leftTime = 0;

	final Runnable myRunnable = new Runnable() {
		public void run() {
			timeLeftTextView.setText(leftTime + "s");
		}
	};

	@Override
	public void onTiming(int leftTime) {
		this.leftTime = leftTime;
		// this is updated in timer thread
		progress.setProgress(leftTime);
		// update view in UI thread
		myHandler.post(myRunnable);
	}

	@Override
	public void onStateChanged(int StateMode) {
		switch (StateMode) {
		case LianlkView.STATE_WIN: {
			// 记录下record
			int record = lianlkView.getTotalTime() - progress.getProgress();
			// 如果成功，记录record
			writeRecord(record);
			Intent intent = new Intent(this, ResultBoardActivity.class);
			intent.putExtra(ResultBoardActivity.KEY_INTENT_MSGCODE, ResultBoardActivity.MSG_CODE_WIN);
			intent.putExtra(ResultBoardActivity.KEY_INTENT_TIMECOST, record);
			startActivity(intent);
			overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
			finish();
			break;
		}
		case LianlkView.STATE_LOSE: {
			int record = lianlkView.getTotalTime() - progress.getProgress();
			Intent intent = new Intent(this, ResultBoardActivity.class);
			intent.putExtra(ResultBoardActivity.KEY_INTENT_MSGCODE, ResultBoardActivity.MSG_CODE_LOSE);
			intent.putExtra(ResultBoardActivity.KEY_INTENT_TIMECOST, record);
			startActivity(intent);
			overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
			finish();
			break;
		}
		case LianlkView.STATE_PAUSE: {
			lianlkView.player.pause();
			lianlkView.stopTimer();
			break;
		}
		}
	}

	private void clearResToQuit() {
		Log.d("LianlkView", "LianlkView cleared!");
		lianlkView.player.release();
		lianlkView.stopTimer();
	}

	@Override
	public void onRefreshed(int count) {
		refreshText.setText(String.valueOf(lianlkView.getRefreshNum()));
	}

	@Override
	public void onHinted(int count) {
		hintText.setText(String.valueOf(lianlkView.getTipNum()));
	}
}